package com.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class RiLiPanel extends JPanel {

	// 年
	private int year = 0;

	// 月
	private int month = 0;

	// 一个离一个的距离
	private int kuan = 50;

	
	//构造方法获得年月
	public RiLiPanel(int year,int month) {
		super();
		this.year=year;
		this.month=month;
	}
	
	/**
	 * 绘制
	 */
	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;

		// 绘制背景
		paintBg(g2d);
		
		//绘制日历
		paintRiLi(g2d);

	}

	// 绘制背景
	public void paintBg(Graphics2D g2d) {

		int x = 0;
		int y = 30;

		// 设置字体颜色
		g2d.setColor(Color.white);
		g2d.setFont(new Font("宋体", 0, 20));

		g2d.drawString("日", x += kuan, y);
		g2d.drawString("一", x += kuan, y);
		g2d.drawString("二", x += kuan, y);
		g2d.drawString("三", x += kuan, y);
		g2d.drawString("四", x += kuan, y);
		g2d.drawString("五", x += kuan, y);
		g2d.drawString("六", x += kuan, y);
	}

	// 绘制日历
	public void paintRiLi(Graphics2D g2d) {
		
		//默认的x
		int defaultX=0;
		
		//横坐标
		int x = defaultX;
		
		//纵坐标
		int y = 60;
		
		// 设置字体
		g2d.setColor(Color.black);
		g2d.setFont(new Font("宋体", 0, 15));	

		int totalYear = 0, totalMonth = 0, totalDay = 0;
		// 计算输入年份距离1900年整年的总天数
		for (int i = 1900; i < year; i++) {
			// 如果该年为闰年，则天数为366，否则365
			if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
				totalYear += 366;
			} else {
				totalYear += 365;
			}
		}
		int days = 0;
		// 计算输入年份的一月到输入月份之前的总天数，并记录最后月份的总天数days
		for (int i = 1; i <= month; i++) {
			switch (i) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				days = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				days = 30;
				break;
			case 2:
				// 计算输入年份二月份的天数
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					days = 29;
				} else {
					days = 28;
				}
				break;
			default:
				break;
			}
			if (i < month) {
				totalMonth += days;
			}
		}
		totalDay = totalYear + totalMonth;
		
		int temp = totalDay % 7 + 1;
		int firstDayofMouth = (temp == 7) ? 0 : temp;

		

		// 计算空格
		x = x + firstDayofMouth * (kuan);
		
		

		// 打印该月份的详细日历
		for (int i = 1; i <= days; i++) {
			
			g2d.drawString(i+"", x+=kuan, y);

			// 每一个礼拜六换行
			if ((totalDay + i) % 7 == 6) {
				x=defaultX;
				y=y+kuan;
			}
		}


	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

}
